var express = require('express');
var router = express.Router();
const addressController = require("../controllers/address_controller")
const mongoUtil = require("../models/mongo_util")
const addressModel = require("../models/address_model")
// 添加收货地址
// 测试数据
// var addressDetail =
//     // "userId": "6182784b3a33092323881598",
//     {
//         "addressId": 5083,
//         "userName": "张三",
//         "userPhone": "15866669999",
//         "defaultFlag": 1,
//         "provinceName": "北京",
//         "cityName": "北京市",
//         "regionName": "东城区",
//         "detailAddress": "天安门"
//     }

router.post("/address", (req, res) => {
    var {
        userid,
        addressDetail
    } = req.body
    if (Object.prototype.toString.call(addressDetail) == '[object Object]' && addressDetail != {}) {
        addressController.AddAddresslist(userid, (result) => {
            if (result.code == 200 && result.data.length > 0) {
                var whereStr = {
                    'userId': userid,
                    // 'goodsId':goodsId
                }
                result.data.forEach((item) => {
                    item.userAddress.unshift(addressDetail)
                    console.log("item.userAddress", item.userAddress);
                    var updateStr = {
                        $set: {
                            'userAddress': item.userAddress
                        }
                    };
                    mongoUtil.getCollection("address").then(collection => {

                        collection.updateOne(whereStr, updateStr, function (err, res) {
                            console.log("添加成功");
                        });
                    })
                })
                res.json({
                    code: 200,
                    err: null,
                    data: [],
                    msg: "添加数据成功"
                })
            } else {
                mongoUtil.getCollection('address').then(collection => {
                    collection.insertOne({
                        userId: userid,
                        userAddress: [addressDetail]
                    }, function (err, res) {});
                })
                res.json({
                    code: 200,
                    err: null,
                    data: [],
                    msg: "添加数据成功"
                })
            }
        })
    } else {
        res.json({
            code: 401,
            data: [],
            msg: "数据必须为对象，且不为空"
        })
        // res.json(result)
    }

})
// 查询用户地址
router.get("/address/:userid", (req, res) => {
    console.log("req.params===", req.params);
    var {
        userid,
    } = req.params
    addressController.getAddresslist(userid, (result) => {
        res.json(result)
    })
})
// 修改用户地址
// 测试数据
// var putaddressDetail =
//     // "userId": "6182784b3a33092323881598",
//     {
//         "addressId": 5083,
//         "userName": "李四",
//         "userPhone": "15866669999",
//         "defaultFlag": 1,
//         "provinceName": "北京",
//         "cityName": "北京市",
//         "regionName": "东城区",
//         "detailAddress": "天安门"
//     }
router.put("/address", (req, res) => {
    var {
        userid,
        // addressId
        // addressId,
        putaddressDetail
    } = req.body
    addressController.putAddresslist(userid, (result) => {

        if (result.code == 200 && result.data.length > 0) {
            var whereStr = {
                'userId': userid,
                // 'goodsId':goodsId
            }
            result.data.forEach((item) => {
                item.userAddress.forEach((ite, index) => {

                    if (ite.addressId == putaddressDetail.addressId) {

                        item.userAddress[index] = putaddressDetail
                        console.log("item.userAddress", item.userAddress);
                        var updateStr = {
                            $set: {
                                'userAddress': item.userAddress
                            }
                        };
                        mongoUtil.getCollection("address").then(collection => {

                            collection.updateOne(whereStr, updateStr, function (err, resp) {
                                console.log("修改成功");
                            });
                        })

                    }
                })
            })
            res.json({

                code: 200,
                data: [],
                msg: "修改用户地址成功"
            })
        } else {
            res.json({
                code: 200,
                data: [],
                msg: "修改用户地址失败"
            })
        }

    })
})
// 删除用户地址
// 测试数据
// var addressId = [5083]
router.delete("/address", (req, res) => {
    var {
        userid,
        addressId
    } = req.body
    addressController.deleteAddresslist(userid, (result) => {
        if (result.code == 200 && result.data.length > 0) {
            var whereStr = {
                'userId': userid,
                // 'goodsId':goodsId
            }
            result.data.forEach(item => {
                const userAddress = item.userAddress.filter(ite => {
                    return !addressId.includes(ite.addressId)
                })
                console.log("item.userAddress", userAddress);
                var updateStr = {
                    $set: {
                        'userAddress': userAddress
                    }
                };
                mongoUtil.getCollection("address").then(collection => {

                    collection.updateOne(whereStr, updateStr, function (err, resp) {
                        console.log("删除成功");
                    });
                })

            })
            res.json({
                code: 200,
                data: [],
                msg: "删除地址成功"
            })
        } else {
            res.json({
                code: 401,
                data: [],
                msg: "没找到该用户"
            })
        }

    })
})
module.exports = router